package com.erdos.core.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.erdos.common.web.BaseAction;
import com.erdos.core.entity.FieldDefinition;
import com.erdos.core.service.FieldDefinitionService;

public class FieldDefinitionAction extends BaseAction<FieldDefinition> {
	private static final Log logger = LogFactory.getLog(FieldDefinitionAction.class);
	private FieldDefinitionService fieldDefinitionService;
	private boolean isUpdate=false;
	
	private String refreshDivID ;

	public FieldDefinitionService getFieldDefinitionService() {
		return fieldDefinitionService;
	}

	public void setFieldDefinitionService(FieldDefinitionService fieldDefinitionService) {
		this.fieldDefinitionService = fieldDefinitionService;
	}
	
	public String input() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String sid = request.getParameter("sid");
		
		if(StringUtils.isEmpty(sid)){
			this.isUpdate=false;
		}
		else	{
			this.isUpdate=true;
		}
		return INPUT;
	}

	public String delete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String sid = request.getParameter("sid");
		this.entity = getBaseService().findByPrimaryKey(new Long(sid));
		int deleteCount = 0 ;
		try{
		deleteCount = getBaseService().delete(entity);
		}catch(Exception er){
//			this.addActionError("删除失败！请稍候再试！");
		    this.addActionError(getText("tips.field.delete.fail"));
		    this.saveBizError(getText("tips.field.delete.fail")) ;
		    return ERROR ;
		}
		request.setAttribute("method", "delete");
		saveBizMessage(getText("tips.delete.success", new String[]{String.valueOf(deleteCount)}));
		return SUCCESS;
	}
	/**
	 * 新增field信息
	 * @return
	 * @author shuwei
	 * @since  2008-7-28
	 */
	public String insert() {
		this.fieldDefinitionService.insert(entity);
		this.getRequest().setAttribute("method", "insert");
		saveBizMessage(getText("tips.insert.success",
				new String[] { String.valueOf(entity.getSid()) }));
		return SUCCESS;
	}
	/**
	 * 编辑Field信息
	 * @return
	 * @author shuwei
	 * @since  2008-7-28
	 */
	public String update() {
		this.fieldDefinitionService.update(entity);
		this.getRequest().setAttribute("method", "update");
		saveBizMessage(getText("tips.update.success",
				new String[] { String.valueOf(entity.getSid()) }));
		return SUCCESS;
	}

	public boolean getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(boolean isUpdate) {
		this.isUpdate = isUpdate;
	}

	public String getRefreshDivID() {
		return refreshDivID;
	}

	public void setRefreshDivID(String refreshDivID) {
		this.refreshDivID = refreshDivID;
	}
}
